const jpress = require('../../utils/jpress.js')
const app = getApp();

Page({
    data: {
        articles: [],
        categories: null,
        slides: [],
        indicatorDots: false,
        autoPlay: false,
        interval: 5000,
        duration: 1000,
        listPageNumber: 1,
        copyright: '',
        showError: "none",
        shoWallDisplay: "none",
        displaySwiper: "none",
    },
    /**  页面加载时触发。一个页面只会调用一次 **/
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: app.globalData.appName,
            success: function (res) {
                // success
            }
        });
        this.setData({
            copyright: app.globalData.copyright,
            topNav: jpress.config.indexNav
        })
        /** 获取首页滑动文章 **/
        this.getSwiperPosts();
        /** 获取文章列表 **/
        this.loadArticleList();
    },
    onReady:function () {
    },
    /** 上拉触底事件 **/
    onReachBottom: function () {
        var self = this;
        if (!self.data.isLastPage) {
            this.loadArticleList(this.data);
        }
        else {
            console.log('最后一页');
        }
    },
    /** 用户下拉动作 **/
    onPullDownRefresh: function () {
        var self = this;
        self.setData({
            showerror: "none",
            showallDisplay: "none",
            isLastPage: false,
            listPageNumber: 1,
            slides: [],
            articles: [],
            copyright: app.globalData.copyright,
            topNav: jpress.config.indexNav
        });
        this.getSwiperPosts();
        this.loadArticleList();
    },
    /** 用户点击页面内转发按钮 **/
    onShareAppMessage: function () {
        return {
            title: '“' + app.globalData.appName +'”网站微信小程序,基于JPress构建.技术支持：'+ jpress.config.host,
            path: 'pages/index/index',
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },
    /** 加载文章列表 **/
    loadArticleList: function (data) {
        wx.showLoading({
            title: '正在加载',
            mask:true
        });
        jpress.getArticlePage({
            page: this.data.listPageNumber,
        }).then(data => {
            this.setData({
                articles: this.data.articles.concat(data.page.list),
                listPageNumber: this.data.listPageNumber+1,
                isLastPage: data.page.lastPage,
            })
        });
        setTimeout(function () {
            wx.hideLoading();
        }, 900);
    },

    /** 获取轮播图 **/
    getSwiperPosts: function () {
        jpress.getSlides().then(data => {
            if(data.value){
                var lines = data.value.split("\n");
                for (var i = 0; i < lines.length; i++) {
                    var imgAndPage = lines[i].split('|');
                    var x = {
                        src: imgAndPage[0],
                        page: imgAndPage[1]
                    }
                    this.data.slides.push(x);
                }
                this.setData({
                    slides: this.data.slides,
                    displaySwiper: "block",
                    shoWallDisplay: "block"
                });
            }else{
                this.setData({
                    showError: "block"
                });
            }
        })
    },
    /** 跳转至查看文章详情 **/
    redirectDetail: function (e) {
        // console.log('查看文章');
        var id = e.currentTarget.id,
            url = '../detail/detail?id=' + id;
        wx.navigateTo({
            url: url
        })
    },
    onShow: function (options){
        wx.setStorageSync('openLinkCount', 0);

    },
    //加载分页
    loadMore: function (e) {
        var self = this;
        if (!self.data.isLastPage){
            self.setData({
                page: self.data.page + 1
            });
            this.fetchPostsData(self.data);
        } else{
            wx.showToast({
                title: '没有更多内容',
                mask: false,
                duration: 1000
            });
        }
    },
    //首页图标跳转
    onNavRedirect:function(e){
        var redicttype = e.currentTarget.dataset.redicttype;
        var url = e.currentTarget.dataset.url == null ? '' : e.currentTarget.dataset.url;
        var appid = e.currentTarget.dataset.appid == null ? '' : e.currentTarget.dataset.appid;
        var extraData = e.currentTarget.dataset.extraData == null ? '' : e.currentTarget.dataset.extraData;
        //跳转到小程序内部页面
        if (redicttype == 'apppage') {
            wx.navigateTo({
                url: url
            })
        //跳转到web-view内嵌的页面
        }else if (redicttype == 'webpage'){
            url = '../webpage/webpage?url=' + url;
            wx.navigateTo({
                url: url
            })
        //跳转到其他app
        }else if (redicttype == 'miniapp'){
            wx.navigateToMiniProgram({
                appId: appid,
                envVersion: 'release',
                path: url,
                extraData: extraData,
                success(res) {
                    // 打开成功
                },
                fail: function (res) {
                    console.log(res);
                }
            })
        }
    }
})